package org.example.java8.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;

public class LambdaUsage2 {
    //Predicate
    private static List<Apple> filter(List<Apple> source, Predicate<Apple> predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : source) {
            if (predicate.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    private static List<Apple> filterByWeight(List<Apple> source, LongPredicate predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : source) {
            if (predicate.test(apple.getWeight())) {
                result.add(apple);
            }
        }
        return result;
    }

    //BiPredicate
    private static List<Apple> filterByBiPredicate(List<Apple> source, BiPredicate<String, Long> predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : source) {
            if (predicate.test(apple.getColor(), apple.getWeight())) {
                result.add(apple);
            }
        }
        return result;
    }

    //Consumer
    private static void simpleTestConsumer(List<Apple> source, Consumer<Apple> consumer) {
        for (Apple a : source) {
            consumer.accept(a);
        }
    }

    private static void simpleTestBiConsumer(String c, List<Apple> source, BiConsumer<Apple, String> consumer) {
        for (Apple a : source) {
            consumer.accept(a, c);
        }
    }

    //Function
    private static String testFunction(Apple apple, Function<Apple, String> function) {
        return function.apply(apple);
    }

    private static Apple testBiFunction(String color, long weight, BiFunction<String, Long, Apple> function) {
        return function.apply(color, weight);
    }

    //Supplier
    private static Apple createApple(Supplier<Apple> supplier) {
        return supplier.get();
    }


    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 170), new Apple("green", 200));
        List<Apple> greenList = filter(list, apple -> apple.getColor().equals("green"));
        //System.out.println(greenList);

        List<Apple> result = filterByWeight(list, w -> w > 100);
        //System.out.println(result);

        List<Apple> result2 = filterByBiPredicate(list, (s, w) -> s.equals("green") && w > 160);
        //System.out.println(result2);

        //simpleTestConsumer(list, a -> System.out.println(a));

        //simpleTestBiConsumer("=color=", list, (a, s) -> System.out.println(s + a.getColor() + " weight:" + a.getWeight()));

        String result3 = testFunction(new Apple("yellow", 100), apple -> apple.getColor());
        //System.out.println(result3);

        //传入int返回泛型
        IntFunction<Double> f = i -> i * 100.0d;
        double result4 = f.apply(10);
        //System.out.println(result4);

        Apple apple = testBiFunction("red", 130, (s, w) -> new Apple(s, w));
        //System.out.println(apple);

        //Supplier
        Supplier<String> s = String::new; //method inference.
        //System.out.println(s.get().getClass());

        Apple a1 = createApple(() -> new Apple("green", 100));
        System.out.println(a1);


    }

}
